package rbac_api

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	rbac_model "kardou/app/system/rbac/internal/model"
)

type userListReq struct {
	Email    string `p:"email"`
	Tel      string `p:"tel"`
	Status   int    `p:"status"`
	Name     string `p:"name"`
	Password string `p:"password"`
	Current  int    `p:"current"`
	PageSize int    `p:"pageSize"`
	ID       int    `p:"id"`
}

type userListRes struct {
	Code     int         `json:"code"`
	Msg      string      `json:"msg"`
	Data     interface{} `json:"data"`
	Total    int         `json:"total"`
	Success  bool        `json:"success"`
	Current  int         `json:"current"`
	PageSize int         `json:"pageSize"`
}

type userListOrm struct {
	rbac_model.User
	Role            string `json:"role"`            //角色
	Permission      string `json:"permistion"`      //权限
	Last_login_time string `json:"last_login_time"` //最后登录时间
}

func (a *userApi) List(r *ghttp.Request) {
	var req *userListReq
	if err := r.Parse(&req); err != nil {
		r.Response.WriteJsonExit(userListRes{
			Code: 0,
			Msg:  err.Error(),
		})
	}

	var data []*userListOrm
	//c,err :=g.Model("user").Count()
	//筛选判断
	q := g.Model("user")
	if req.ID != 0 {
		q.Where("id=?", req.ID)
	}
	if req.Name != "" {
		q.Where("name=?", req.Name)
	}
	if req.Email != "" {
		q.Where("email=?", req.Email)
	}
	if req.Tel != "" {
		q.Where("tel=?", req.Tel)
	}
	if req.Status != 0 {
		q.Where("status=?", req.Status)
	}
	t, _ := q.Count()
	err := q.Page(req.Current, req.PageSize).Order("id","desc").Scan(&data)
	if err != nil {
		r.Response.WriteJsonExit(userListRes{
			Code: 0,
		})
	}
	//处理其他字段
	for _, item := range data {
		item.GenStatusText()
	}
	g.Log().Header(false).Println(data)


	r.Response.WriteJsonExit(userListRes{
		Code:     1,
		Data:     data,
		Total:    t,
		Success:  true,
		Current:  1,
		PageSize: 2,
	})
}
